//
//  MyOrderViewCell.m
//  LiverClient
//
//  Created by dongshangtong on 16/5/9.
//  Copyright © 2016年 朱亚杰. All rights reserved.
//

#import "MyOrderViewCell.h"

#import "YCOrderModel.h"

//#import "MJPhotoBrowser.h"
//#import "MJPhoto.h"

//黄色按钮
#define BUTTON_YELLOW RGB(253, 132, 0)
//灰色按钮
#define BUTTON_GRAY RGB(153, 153, 153)



@interface MyOrderViewCell()
@property (weak, nonatomic) IBOutlet UIImageView *hosIcon;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UILabel *Paystatus;

@property (weak, nonatomic) IBOutlet UIButton *waitWriteOff;

@property (weak, nonatomic) IBOutlet UILabel *preduotName;
@property (weak, nonatomic) IBOutlet UILabel *detaileAddress;
@property (weak, nonatomic) IBOutlet UILabel *provinces;
@property (weak, nonatomic) IBOutlet UILabel *payMonery;

- (IBAction)confirmClick:(UIButton *)sender;

@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;
@property (weak, nonatomic) IBOutlet UILabel *orderId;
@property (weak, nonatomic) IBOutlet UILabel *createDate;


@end

@implementation MyOrderViewCell

+(instancetype)myOrderViewCellWithTableView:(UITableView *)tableView
{
    static NSString *cellID = @"MyOrderViewCell";
    
    MyOrderViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell) {
        
        cell = [[MyOrderViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    return cell;
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.hosIcon.layer.cornerRadius= 6;
    self.hosIcon.clipsToBounds =YES;
    
    [self setButton:self.confirmBtn CornerRadius:6 BorderWidth:1 ClipsToBounds:YES];
    [self setButton:self.waitWriteOff CornerRadius:6 BorderWidth:1 ClipsToBounds:YES];
    self.waitWriteOff.hidden = YES;

}

-(void)setOrder:(YCOrderModel *)order
{
    _order= order;
    
    NSURL *url = [NSURL URLWithString:order.servePic];
    
    [self.hosIcon sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"listplace"]];
    
    self.preduotName.text = order.sjName;
    self.detaileAddress.text = order.sjName;
    
    self.Paystatus.text = order.orderStatus;
   
    self.createDate.text=[NSString stringWithFormat:@"时间：%@",order.createDate];
    self.nameLabel.text = [NSString stringWithFormat:@"姓名：%@",order.memberName];
    /*
     如果订单状态是已付款，那么已付款按钮变成灰色并且不能点击,而核销按钮可以去核销；
     */
    if (order.YCOrderStatus==YCOrderStatus_DidPay){
        //已付款按钮变灰色不能点击
        [self setButton:self.confirmBtn title:@"再次购买" TitleColor:GreenColor BorderColor:GreenColor  hidden:NO clickEnable:BUTTON_YELLOW_STATUS];
        self.Paystatus.text=@"待付款";
        
        self.Paystatus.textColor = [UIColor colorWithCommonType:CommonColorGreen];
       }
    //如果订单状态是待付款,那么核销按钮便成灰色不能点击，付款按钮便成去支付
    else if(order.YCOrderStatus==YCOrderStatus_WaitPay)
    {
        //去支付按钮变成可点击
        [self setButton:self.confirmBtn title:@"快速支付" TitleColor:GreenColor BorderColor:GreenColor hidden:NO clickEnable:BUTTON_YELLOW_STATUS];
        self.Paystatus.text=@"待付款";
        self.Paystatus.textColor = [UIColor colorWithCommonType:CommonColorOrange];
    }
    
    
    //如果订单状态是消费成功的状态
    else if(order.YCOrderStatus==YCOrderStatus_Success)
    {
        //去支付按钮变成灰色不可点击状态
        [self setButton:self.confirmBtn title:@"已付款" TitleColor:BUTTON_GRAY BorderColor:BUTTON_GRAY hidden:NO clickEnable:BUTTON_GRAY_STATUS];
        //订单状态变成已取消
        self.Paystatus.text=@"已付款";
        self.Paystatus.textColor = [UIColor colorWithCommonType:CommonColorGreen];
    }
    
}

-(void)setButton:(UIButton*)button title:(NSString*)title TitleColor:(UIColor*)titleColor BorderColor:(UIColor*)borderColor hidden:(BOOL)hidden clickEnable:(BUTTON_TAG)buttonTag
{
    button.layer.borderColor=borderColor.CGColor;
    button.tag=buttonTag;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    button.hidden=hidden;
}
 // cell 与 cell 之间的分隔线设定为 10 ；
//
//-(void)setFrame:(CGRect)frame
//{
//    static CGFloat margin = 10;
//    frame.size.height -= margin;
//    frame.origin.y += margin;
//    [super setFrame:frame];
//}

// 点击待核销进入待核销界面

- (IBAction)waitWriteOff:(UIButton *)sender {
if (self.delegate && [self.delegate respondsToSelector:@selector(myOrderViewCellToPayOrder:payType:)]) {
    if (sender.tag==BUTTON_YELLOW_STATUS) {   // ---> 灰色按钮的点击效果无效
        [self.delegate myOrderViewCellToPayOrder:_order payType:YCPUSHTYPE_CHECK];
    }
  }
}

// 点击支付按钮销进入支付界面
// 目前右边按钮只能被支付按钮所替代，其它的以后在说

- (IBAction)confirmClick:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(myOrderViewCellToPayOrder:payType:)]) {
        if (sender.tag==BUTTON_YELLOW_STATUS) {   // ---> 灰色按钮的点击效果无效
        [self.delegate myOrderViewCellToPayOrder:_order payType:YCPUSHTYPE_PAY];
        }
    }
  }
/*
 
 设置button的圆角和边框
 
 */
-(void)setButton:(UIButton *)button CornerRadius:(CGFloat)cornerRadius BorderWidth:(CGFloat)borderWidth ClipsToBounds:(BOOL)clipsToBounds
{
    button.layer.cornerRadius= cornerRadius;
    button.layer.borderWidth = borderWidth;
    button.clipsToBounds = clipsToBounds;
}
@end
















